
package org.openeai.lite.console.beans;

import java.io.Serializable;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import org.apache.log4j.Logger;
import org.openeai.lite.common.control.RouterController;
import org.openeai.lite.common.exception.OpenEAIException;
import org.openeai.lite.common.util.Constants;
import org.openeai.lite.common.util.DateTimeUtils;


@ManagedBean(name = "routerBean")
@SessionScoped
public class RouterBean implements Serializable
{

    private static Logger log = Logger.getLogger(RouterBean.class);

    private static final long serialVersionUID = 1L;

    // Properties
    private boolean started;

    private String startdate;

    private String status = Constants.INACTIVE;

    // Started?
    public boolean isStarted()
    {
        try
        {
            started = RouterController.getInstance().isStarted();
        }
        catch (OpenEAIException oue)
        {
            // Logging and exception handling should be done in controller
            ;
        }
        return started;
    }


    // Status?
    public String getStatus()
    {
        try
        {
            if (RouterController.getInstance().isStarted())
            {
                this.status = Constants.ACTIVE;
            }
            else
            {
                this.status = Constants.INACTIVE;
            }
        }
        catch (OpenEAIException oue)
        {
            // Logging and exception handling should be done in controller
            ;
        }

        return status;
    }

    public void setStatus(String status)
    {
        if (status.equals(Constants.ACTIVE))
        {
            this.status = Constants.ACTIVE;
        }
        else if (status.equals(Constants.INACTIVE))
        {
            this.status = Constants.INACTIVE;
        }
        else
        {
            log.error("Cannot determine status of message router!");
        }
    }

    // Camel startup date/time, nicely formatted
    public String getStartdate()
    {
        try
        {
            Long date = RouterController.getInstance().getContext().getStartupDate();
            this.startdate = new DateTimeUtils().millisToFormattedDate(date);
        }
        catch (Exception e)
        {
            // Camel probably down
            this.startdate = Constants.UNKNOWN;
        }
        return startdate;
    }


}
